<template>
	<view class="page">
		<OrgIndex v-if="userInfo.userType == enums['账号类型']['机构']&&login==1" ref="orgIndexRef"></OrgIndex>
		<UserIndex v-if="userInfo.userType == enums['账号类型']['用户']" ref="userIndexRef"></UserIndex>
	</view>
</template>

<script>
	import UserIndex from './userIndex.vue';
	import OrgIndex from './orgIndex.vue';
	import * as commonApi from '@/api/common.js';
	export default {
		components:{
			UserIndex,
			OrgIndex
		},
		data() {
			return {
				userInfo:{},
				enums: this.$enums,
				user:null,
				login:0
			}
		},
		created() {
			let userDate={
				age: null,
				id: 3,
				instituteId: null,
				isUser: 1,
				juzhudi: '大曌',
				jwt: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJ7XCJ1c2VySWRcIjozLFwidXNlclR5cGVcIjowfSJ9.h8FMVW7sVEYQYW-XKi01i7EMQ1Ea0qwtiJFnqc1Hub8',
				openId: 'sf12df54sd56fsd32fds2f1',
				owner: null,
				phone: '',
				sex: 1,
				shequ: null,
				userType: 0,
				username: 'yao用户',
				wxAvatar: '',
				wxNickname: ''
				
			}
			console.log('App Launch');
			let a=uni.getStorageSync('open')
			if(a.wxNickname){
				console.log('用户信息',a)
				this.$store.dispatch('wechatLogin', a).then(res => {
				});
				console.log('真登录')
				this.login=1
			}else{
				uni.setStorageSync('open',userDate)
				this.$store.dispatch('wechatLogin', userDate).then(res => {
				});
				console.log('假登录')
				this.login=1
			}
			
		},
		onShow() {
			
			this.getOrnOrderList();
			// this.getUserInfo();
			
			this.userInfo=uni.getStorageSync('open')
			console.log(this.userInfo)
			if(this.userInfo.wxAvatar!=''){
				this.uploadUserInfo();
			}
			
			this.getUserIndexData();
			// this.gologin()
			
		},
		methods: {
			gologin(){
				this.user=uni.getStorageSync('open')
				setTimeout(()=>{
					if(this.user.length<=0){
						uni.navigateTo({
							url:'../system/login',
						})
					}
					console.log(this.user)
				},2000)
				
				
			},
			getUserIndexData(){
				setTimeout( () => {
					if (this.userInfo.userType == this.enums['账号类型']['用户']) {
						this.$refs.userIndexRef.showInit();
					}
				}, 100) 
			},
			uploadUserInfo() {
				if(this.userInfo.jwt){
					if (this.userInfo.userType == this.enums['账号类型']['用户']) {
						commonApi
							.getAgedByToken({})
							.then(res => {
								if (res.code === 0) {
									this.$store.dispatch('wechatLogin', res.data).then(res => {})
								}
							})
							.catch(e => {
								uni.showToast({
									title: e.msg,
									icon: 'none',
									duration: 1000
								});
							});
					}
				}
			},
			getOrnOrderList(){
				setTimeout( () => {
					if(this.userInfo.userType == this.enums['账号类型']['机构']) {
						this.$refs.orgIndexRef.reset();
					}
				}, 100) 
			},
			getUserInfo() {
				this.userInfo = this.$store.getters.getUserInfo;
			},
		}
	}
</script>

<style lang="stylus">
</style>
